package per.wsj.paging;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.paging.PagedListAdapter;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

public class StudentAdapter extends PagedListAdapter<StudentModel, StudentAdapter.CustomViewHolder> {

    private static DiffUtil.ItemCallback<StudentModel> DIFF_CALLBACK = new DiffUtil.ItemCallback<StudentModel>() {
        @Override
        public boolean areItemsTheSame(StudentModel oldConcert, StudentModel newConcert) {
            return oldConcert.getId() == newConcert.getId();
        }

        @Override
        public boolean areContentsTheSame(StudentModel oldConcert,
                                          StudentModel newConcert) {
            return oldConcert.equals(newConcert);
        }
    };

    protected StudentAdapter() {
        super(DIFF_CALLBACK);
    }

    @NonNull
    @Override
    public CustomViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new CustomViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_test, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull CustomViewHolder holder, int position) {
        StudentModel studentModel = getItem(position);
        holder.tvID.setText(String.valueOf(studentModel.getId()));
        holder.tvName.setText(studentModel.getName());
    }

    public class CustomViewHolder extends RecyclerView.ViewHolder {
        TextView tvID, tvName;

        public CustomViewHolder(@NonNull View itemView) {
            super(itemView);
            tvID = itemView.findViewById(R.id.tv_id);
            tvName = itemView.findViewById(R.id.tv_name);
        }
    }
}
